
package fileby.gui.sidebar;

import fileby.core.IDisplayableFile;
import fileby.core.SourceType;
import fileby.ctrl.transferhandlers.ITransferHandlerProvider;
import fileby.core.TransferHandlerType;
import fileby.util.properties.IPropertyBundleProvider;
import fileby.util.properties.PropertyBundle;
import java.awt.Graphics;
import java.awt.Insets;
import java.util.List;
import javax.swing.Icon;
import javax.swing.border.EmptyBorder;

/**
 *
 * @author Oscar
 */
public class SideBarPanel extends javax.swing.JPanel {

    private SourceType sourceType;
    private ISideBarProvider sideBarProvider;
    private IPropertyBundleProvider propertyBundleProvider;
    private ITransferHandlerProvider transferHandlerProvider;
    private Icon background_north;
    private Icon background_south;

    public SideBarPanel(SourceType sourceType, 
            ISideBarProvider sideBarProvider,
            IPropertyBundleProvider propertyBundleProvider,
            ITransferHandlerProvider transferHandlerProvider) {

        this.propertyBundleProvider = propertyBundleProvider;
        this.sideBarProvider = sideBarProvider;
        this.transferHandlerProvider = transferHandlerProvider;
        this.sourceType = sourceType;
        initComponents();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        background_north.paintIcon(northPanel, g, 5, 0);
        g.drawLine(5, 30, 5, southPanel.getY());
        g.drawLine(199, 30, 199, southPanel.getY());
        background_south.paintIcon(southPanel, g, 5, southPanel.getY());
    }

    public void updateSidebarItems(List<IDisplayableFile> files) {
        contents.removeAll();
        for (IDisplayableFile f : files)
            contents.add(sideBarProvider.createItem(f, sourceType));
        revalidate();
        repaint();
    }
    
    private void initAsDevice() {
        title.setText("Devices");
    }

    private void initAsFavorite() {
        this.setTransferHandler(transferHandlerProvider.create(TransferHandlerType.FAVORITE));
        title.setText("Favorites");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        northPanel = new javax.swing.JPanel();
        title = new javax.swing.JLabel();
        southPanel = new javax.swing.JPanel();
        centerScrollPane = new javax.swing.JScrollPane();
        contents = new javax.swing.JPanel();

        PropertyBundle bundle = propertyBundleProvider.create(this.getClass());
        background_north = bundle.getIcon("north.icon");
        background_south = bundle.getIcon("south.icon");
        setBackground(bundle.getColor("Form.background")); // NOI18N
        setAutoscrolls(true);
        setInheritsPopupMenu(true);
        setMaximumSize(new java.awt.Dimension(200, 2147483647));
        setMinimumSize(new java.awt.Dimension(200, 57));
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(200, 35));
        setVerifyInputWhenFocusTarget(false);
        setLayout(new java.awt.BorderLayout());

        northPanel.setMaximumSize(new java.awt.Dimension(195, 30));
        northPanel.setMinimumSize(new java.awt.Dimension(195, 30));
        northPanel.setOpaque(false);
        northPanel.setPreferredSize(new java.awt.Dimension(195, 30));

        title.setFont(bundle.getFont("title.font")); // NOI18N

        javax.swing.GroupLayout northPanelLayout = new javax.swing.GroupLayout(northPanel);
        northPanel.setLayout(northPanelLayout);
        northPanelLayout.setHorizontalGroup(
            northPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(northPanelLayout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(298, Short.MAX_VALUE))
        );
        northPanelLayout.setVerticalGroup(
            northPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, northPanelLayout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 14, Short.MAX_VALUE)
                .addGap(7, 7, 7))
        );

        add(northPanel, java.awt.BorderLayout.NORTH);

        southPanel.setMaximumSize(new java.awt.Dimension(195, 5));
        southPanel.setMinimumSize(new java.awt.Dimension(195, 5));
        southPanel.setOpaque(false);
        southPanel.setPreferredSize(new java.awt.Dimension(195, 5));
        add(southPanel, java.awt.BorderLayout.SOUTH);

        centerScrollPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 1, 0, 1));
        centerScrollPane.setOpaque(false);

        contents.setBackground(bundle.getColor("contents.background")); // NOI18N
        contents.setLayout(new javax.swing.BoxLayout(contents, javax.swing.BoxLayout.PAGE_AXIS));
        centerScrollPane.setViewportView(contents);

        add(centerScrollPane, java.awt.BorderLayout.CENTER);
        setBorder(new EmptyBorder(new Insets(0,5,0,0)));

        if(sourceType.equals(SourceType.FAVORITE))
            initAsFavorite();
        else
            initAsDevice();

    }// </editor-fold>

    private javax.swing.JScrollPane centerScrollPane;
    private javax.swing.JPanel contents;
    private javax.swing.JPanel northPanel;
    private javax.swing.JPanel southPanel;
    private javax.swing.JLabel title;

}
